<?php

namespace App\Http\Controllers;

use App\Models\Property;
use App\Models\User;
use App\Models\UserGrade;
use App\Models\Coupon;
use Exception;
use Illuminate\Http\Request;

/**
 * 团利会员
 */
class MemberController extends Controller
{
    /**
     * 会员水滴值
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function member_info(Request $request)
    {
        try {
            $status = 0;
            $data   = [];
            $userID = $request->get('user_id');
            if (empty($userID)) {
                throw new Exception('缺少参数');
            }
            $user = User::where(['userID' => $userID])->first();
            //会员信息
            $data['user_id']  = $user->userID;
            $data['nickname'] = $user->nickname;
            $data['headimg']  = $user->headimg;
            $data['rank']     = $user->rank;
            //当前水滴值
            $property      = Property::where(['userID' => $userID, 'type' => 5])->first();
            $data['value'] = intval($property->value / 100);
            //累计水滴值
            $data['grand_total'] = intval($property->grand_total / 100);
            $data['below']       = UserGrade::where(['id' => $user->rank])->value('value_below');
            $data['up']          = UserGrade::where(['id' => $user->rank])->value('value_up');
            //获取下一级的会员需要水滴值下限
            $data['next_below'] = UserGrade::where(['id' => ($user->rank + 1)])->value('value_below');
            $data['next_up']    = UserGrade::where(['id' => ($user->rank + 1)])->value('value_up');
            //优惠券生成记录
            $data['coupons'] = Coupon::where(['id' => $userID])
            	->select('coupon_name','coupon_id','num','coupon_type')
            	->get();

            $status = 1;
            $msg    = '查询成功';
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }
}
